//
//  ZXSortViewModel.swift
//  loveBee
//
//  Created by zhangxing on 2017/8/18.
//  Copyright © 2017年 zhangXing. All rights reserved.
//

import UIKit
//加载网络数据
class ZXSortViewModel: NSObject {
    
    //引用左边ZXSortLeftCellModel的模型数组
    var sortLeftCellModelList: [ZXSortLeftCellModel]?
    
    //右边封装所有的viewModel数组的这样一个数组[[viewModel]]
    var allRightViewModelList: [[ZXSortRightCellViewModel]] = [[ZXSortRightCellViewModel]]()
    
    //处理数据方法
    func handleCellData(callBack: @escaping(Bool) -> ()) {
        
        ZXNetWorkingManager.sharedManager.gainAppInfo(URLStr: "http://iosapi.itcast.cn/loveBeen/supermarket.json.php", call: "5") { (response, error) in
            if error != nil {
                
                print("数据获取失败")
                //失败回调
                callBack(false)
                return
            }
            else {
                //数据获取成功
//                print(response!)
                /*
    {
        code = 0;
        data =     {
            categories =(
                            {
                                "disabled_show" = 0;
                                icon = "/upload/activity/2015071416553788.png";
                                id = a106;
                                name = "\U4f18\U9009\U6c34\U679c";
                                pcid = 55;
                                sort = 1;
                                visibility = 1;
                            },
                                                                            
                            {
                                "disabled_show" = 0;
                                icon = "";
                                id = a134;
                                name = "\U751f\U6d3b\U7528\U54c1";
                                pcid = 55;
                                sort = 13;
                                visibility = 1;
                            }
                        );
                 
                 
                 
                 
             products = {
                        a106=(
                                 {
                                 attribute = "";
                                 "brand_id" = 128;
                                 "brand_name" = "\U7231\U9c9c\U8702";
                                 "cart_group_id" = 0;
                                 "category_id" = 106;
                                 cid = 106;
                                 "dealer_id" = 7951;
                                 "had_pm" = 0;
                                 "hot_degree" = 0;
                                 id = 10229;
                                 img = "http://img01.bqstatic.com/upload/goods/000/001/0229/0000010229.jpg@200w_200h_90Q.jpg";
                                 "is_xf" = 1;
                                 ismix = 0;
                                 "market_price" = "15.00";
                                 name = "\U7231\U9c9c\U8702\U00b7\U6cf0\U56fd\U9f99\U773c";
                                 number = 7;
                                 "partner_price" = "9.90";
                                 pcid = 55;
                                 "pm_desc" = "";
                                 "pre_img" = "0000010229.jpg";
                                 "pre_imgs" = "0000010229_18468.jpg,";
                                 price = "9.90";
                                 "product_id" = 10229;
                                 "safe_day" = 0;
                                 sort = 0;
                                 "source_id" = 1;
                                 specifics = "400g/\U76d2";
                                 "store_nums" = 50;
                                 "tag_ids" = 5;
                                 },
                            )
                         
                         
                         
                        a158=(
                                 {
                                 attribute = "";
                                 "brand_id" = 128;
                                 "brand_name" = "\U7231\U9c9c\U8702";
                                 "cart_group_id" = 0;
                                 "category_id" = 158;
                                 cid = 158;
                                 "dealer_id" = 7951;
                                 "had_pm" = 1;
                                 "hot_degree" = 0;
                                 id = 8736;
                                 img = "http://img01.bqstatic.com/upload/goods/000/000/8736/0000008736.jpg@200w_200h_90Q.jpg";
                                 "is_xf" = 1;
                                 ismix = 0;
                                 "market_price" = "7.50";
                                 name = "\U7231\U9c9c\U8702\U00b7\U5343\U79a7\U679c";
                                 number = 3;
                                 "partner_price" = "7.50";
                                 pcid = 55;
                                 "pm_desc" = "\U9650\U65f6\U7279\U4ef7";
                                 "pre_img" = "0000008736.jpg";
                                 "pre_imgs" = "0000008736_67950.jpg,";
                                 price = "7.50";
                                 "product_id" = 8736;
                                 "safe_day" = 0;
                                 sort = 20;
                                 "source_id" = 1;
                                 specifics = 350g;
                                 "store_nums" = 50;
                                 "tag_ids" = 5;
                                 },
                            )
                        */
                
                //response是一个字典
                var dictionary = response as! [String: Any]
                
                //通过键值data获取一个字典
                var dataDict = dictionary["data"] as! [String: Any]
                
                //通过键值categories获取一个字典数组即分类
                let categoriesDicArray = dataDict["categories"] as! [[String: Any]]
                
                //通过键值products得到一个字典
                let productDicArray = dataDict["products"] as! [String: Any]
                
                
                //通过yymodel来讲字典数组转成模型数组(接下来我们需要创建leftCell模型)
                self.sortLeftCellModelList = NSArray.yy_modelArray(with: ZXSortLeftCellModel.self, json: categoriesDicArray) as? [ZXSortLeftCellModel]
                
                
                //为什么要遍历左边的模型数组,因为左边有多少行,右边就有多少组
                for leftCellModel in self.sortLeftCellModelList! {
                    //获取左边cell模型的id,通过id来获取右边cell的字典数组,因为这个id值就是右边products这个字典中的键值
                    let leftCellID = leftCellModel.id
                    
                    //通过左边cell的id获取到右边cell的id字典数组
                    let idDictArray = productDicArray[leftCellID!] as! [[String: Any]]
                    
                    //接下来需要通过yymodel来将字典转模型转成模型数组
                    let modelArray = NSArray.yy_modelArray(with: ZXSortRightCellModel.self, json: idDictArray)
                    
                    
                    //现在我们将获取到的模型数组传递值给viewModel中,让viewModel来对数据做逻辑处理
                    //首先创建一个viewModel数组
                    var rightCellViewModelArray = [ZXSortRightCellViewModel]()
                    
                    //遍历模型数组
                    for model in modelArray! {
                        
                        let rightCellViewModel = ZXSortRightCellViewModel()
                        
                        rightCellViewModel.sortRightCellModel = model as? ZXSortRightCellModel
                        
                        rightCellViewModelArray.append(rightCellViewModel)
                    }
                    
                    //我们将遍历出来的viewModel数组添加到一个更大的数组中[[viewModel]],此时我们需要在前面再定义一个[[viewModel]]数组
                    self.allRightViewModelList.append(rightCellViewModelArray)
                    
                    
                    
                }
                
                
                //成功回调
                callBack(true)
            }
        }
    }

}
